const { default: json } = require("../../../towxml/parse/highlight/languages/json");
var http = require('../../../utils/http')
const app = getApp()
const ipconfig = app.globalData.ipconfig;
Page({
    data: {
        friendData: {},
        message: '',
        self: {},
        record: {},
        records: {},
        scrollToView: '',
        message1:{},
        temp:0
    },
    getData(item) {
        this.setData({
            friendData: JSON.parse(decodeURIComponent(item.item))
        })
    },
    onLoad(options) {
        const that = this;
        this.getData(options),
        this.setData({
            self: wx.getStorageSync('userInfo'),
        })
        this.setData({
            'record.userId': this.data.self.id,
            'record.userName': this.data.self.username,
            'record.userAvatar': this.data.self.avatar,
            'record.friendId': this.data.friendData.id,
        })
        this.getTitle()
        this.findRecords()
    },
    onShow(){
        this.onSoekctCallback();
    },
    // 收到消息处理
    onSoekctCallback: function () {
        const that = this;
        app.globalData.callback = (res) => {
            const data2 = JSON.parse(res.data)
            if (data2.from) {
                that.findRecords();
            }
        }
    },
    //给头部添加好有姓名
    getTitle() {
        const that = this;
        console.log(that);
        if (this.data.self.id == this.data.friendData.userAId) {
            wx.setNavigationBarTitle({
                title: this.data.friendData.userBNickname
            })
        } else {
            wx.setNavigationBarTitle({
                title: this.data.friendData.userANickname
            })
        }
    },
    //发送消息
    addRecord() {
        this.setData({
            'record.text': this.data.message,
            'message1.text': this.data.message
        })
        if (this.data.self.id == this.data.friendData.userAId) {
            this.setData({
                'message1.to': this.data.friendData.userBId
            })
        }else{
            this.setData({
                'message1.to': this.data.friendData.userAId
            })
        }
        const msg = JSON.stringify(this.data.message1)
        const that = this;
        http.post("/mini/addRecord",that.data.record,res=>{
            that.setData({
                message: ''
            })
            wx.sendSocketMessage({
                data: msg,
                success: function (res) {
                    console.log(res);
                },
                fail: function (res) {}
            })
             that.findRecords()
        })
    },
    //从数据库查找聊所有与这个好友的聊天记录
    findRecords() {
        const that = this;
        http.get(`/mini/findRecords/${that.data.friendData.id}`,{},res=>{
            that.setData({
                records: res.data.data
            })
            that.setData({
                scrollToView: 'scrollToView'
            })
        })
       
    },
    onReady(){
        this.setData({
            scrollToView: 'scrollToView'
        })

    },
    // 滚动到页面底部
    pageScrollToBottom: function () {
        let that = this;
        wx.createSelectorQuery().select('#x_chat').boundingClientRect(function (rect) {
            console.log(rect)
            wx.pageScrollTo({
                scrollTop: rect.height,
                duration: 0
            })
        }).exec()
    },
})